package cn.jiedanba.cacert.caadmin.controller;

import java.io.ByteArrayInputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jiedanba.cacert.admin.service.CAService;
import cn.jiedanba.cacert.admin.service.KeyUsageService;
import cn.jiedanba.cacert.admin.vo.IssueRootCaVo;
import cn.jiedanba.cacert.admin.vo.RevokeCaVo;
import cn.jiedanba.cacert.common.result.ResponseResult;
import cn.jiedanba.cacert.model.KeyUsage;
import cn.jiedanba.cacert.model.RootCa;

/**
 * CA管理
 * 
 * @author
 *
 */
@Controller
@RequestMapping("/ca")
public class CAController extends BaseController {

	@Resource
	private CAService caService;
	@Resource
	private KeyUsageService keyUsageService;

	private static final String prefix = "ca/";

	/**
	 * 根证书管理
	 * 
	 * @return
	 */
	@GetMapping("/rootCa")
	public String certList() {
		return prefix + "rootCa";
	}

	/**
	 * 根证书列表
	 * 
	 * @return
	 */
	@PostMapping("/rootCaList")
	@ResponseBody
	public List<RootCa> rootCaList(RootCa ca) {
		return caService.selectRootCaList(ca);
	}

	/**
	 * 下载签名证书
	 *
	 * @return
	 */
	@GetMapping("/getSignCert")
	@ResponseBody
	public ResponseEntity<InputStreamResource> downloadSignCert(String id) {
		ResponseResult down = caService.downloadSignCert(id);
		ModelMap data = (ModelMap) down.getData();
		ByteArrayInputStream bais = new ByteArrayInputStream((byte[]) data.get("cert"));
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", data.get("caName") + ".cer"));
		return ResponseEntity.ok().headers(headers).contentType(MediaType.parseMediaType("application/octet-stream"))
				.body(new InputStreamResource(bais));
	}

	/**
	 * 颁发根证书
	 * 
	 * @return
	 */
	@GetMapping("/issueRootCa")
	public String issueRootCaView(String id) {
		if (StringUtils.isNotBlank(id)) {
			getRequest().setAttribute("issueCa", caService.selectRootCaById(id));
		}
		getRequest().setAttribute("enhanceUsageList",
				keyUsageService.selectKeyUsageList(new KeyUsage().setEnhance(true)));
		return prefix + "issueRootCa";
	}

	/**
	 * 构建证书主题
	 * 
	 * @return
	 */
	@GetMapping("/buildSubjectDn")
	public String buildSubjectDn() {
		return prefix + "buildSubjectDn";
	}

	/**
	 * 颁发根证书
	 * 
	 * @param rootCa
	 * @return
	 */
	@PostMapping("/issueRootCa")
	@ResponseBody
	public ResponseResult issueRootCa(@Valid IssueRootCaVo vo) {
		return caService.issueRootCa(vo);
	}

	/**
	 * 吊销CA证书
	 * 
	 * @param vo
	 * @return
	 */
	@GetMapping("/revokeCa")
	public String revokeCaView(String id) {
		getRequest().setAttribute("rootId", id);
		return prefix + "revokeCa";
	}

	/**
	 * 吊销CA证书
	 * 
	 * @param vo
	 * @return
	 */
	@PostMapping("/revokeCa")
	@ResponseBody
	public ResponseResult revokeCa(@Valid RevokeCaVo vo) {
		return caService.revokeCa(vo);
	}

}
